<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Test libmacrame - Core-JS</title>
    <link rel="stylesheet" href="common.css" type="text/css">

    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript" src="../src/core.js"></script>
    <script type="text/javascript" src="../src/core-js.js"></script>

    <script type="text/javascript"><!--
/*
 * Test and display which methods are native or not from the global JS objects.
 */
tests.corejs_native = function () {
  $t.check_native(
    Object, $.js.Object, 'Object',
    Array.prototype, $.js.Array, 'Array.prototype',
    String.prototype, $.js.String, 'String.prototype'
  );
};

/*
 * Test the Array methods, native or not.
 */
tests.corejs_array = function () {
  $t.output('Testing .every() ...');

  var nr_lt_5 = function (val) {
    return val < 5;
  };

  var arr1 = [1, 2, 3, 4];

  if (!arr1.every(nr_lt_5)) {
    $t.report_error('arr1.every(nr_lt_5) failed: it must return true.');
  }

  var arr2 = [1, 2, 3, 4, 5];
  if (arr2.every(nr_lt_5)) {
    $t.report_error('arr2.every(nr_lt_5) failed: it must return false.');
  }

  $t.output('Testing .forEach() ...');

  arr1 = ['a', 2, 'c', 4];
  arr2 = [];

  arr1.forEach(function (val, i, arr) {
    arr2[i] = arr[i];
  });

  // Check result using .every() that the values are the same in arr2.
  var res = arr2.every(function (val, i) {
    return arr1[i] === val;
  });

  if (!res) {
    $t.report_error('arr2.forEach() failed.');
  }

  $t.output('Testing .some() ...');

  arr1 = [10, 23, 34, 4, 5];
  if (!arr1.some(nr_lt_5)) {
    $t.report_error('arr1.some(nr_lt_5) failed: it must return true.');
  }

  arr2 = [10, 23, 34, 41, 5];
  if (arr2.some(nr_lt_5)) {
    $t.report_error('arr2.some(nr_lt_5) failed: it must return false.');
  }

  $t.output('Testing .filter() ...');

  arr1 = [5, 3, 7, 8, 4, 10, 1];
  arr2 = arr1.filter(nr_lt_5);

  $t.check_cond(arr2.length, 3, '==');

  if (!arr2.every(nr_lt_5)) {
    $t.report_error('arr1.filter(nr_lt_5) failed.');
  }

  $t.output('Testing .map() ...');

  var map_fn = function (val, i) {
    return val*i;
  };

  arr2 = arr1.map(map_fn);

  res = arr1.every(function (val, i) {
    return arr2[i] === val*i;
  });

  if (!res) {
    $t.report_error('arr1.map(map_fn) failed.');
  }

  $t.output('Testing .reduce() and .reduceRight() ...');

  var sum_fn = function (last, val, i) {
    return last+val+i;
  };

  arr1 = ['a', 'b', 'c', 'd'];
  if (arr1.reduce(sum_fn, '-') != '-a0b1c2d3') {
    $t.report_error('arr1.reduce(sum_fn) failed.');
  }

  if (arr1.reduceRight(sum_fn, '-') != '-d3c2b1a0') {
    $t.report_error('arr1.reduceRight(sum_fn) failed.');
  }

  $t.output('Testing .indexOf() and .lastIndexOf() ...');

  arr1 = ['b', 'c', 'a', 'f', 'g', 'h', 'a', 'e'];
  if (arr1.indexOf('a') != 2) {
    $t.report_error("arr1.indexOf('a') failed.");
  }
  if (arr1.indexOf('z') != -1) {
    $t.report_error("arr1.indexOf('z') failed.");
  }
  if (arr1.lastIndexOf('a') != 6) {
    $t.report_error("arr1.lastIndexOf('a') failed.");
  }
  if (arr1.lastIndexOf('z') != -1) {
    $t.report_error("arr1.lastIndexOf('z') failed.");
  }
};

/*
 * Test the Object methods, native or not.
 */
tests.corejs_object = function () {
  $t.output('Testing .every() ...');

  var nr_lt_5 = function (val) {
    return val < 5 ? true : false;
  };

  var obj1 = {a : 0, b : 1, c : 2, d : 3, e : 4};

  if (!Object.every.call(obj1, nr_lt_5)) {
    $t.report_error("Object.every.call(obj1, nr_lt_5) failed.");
  }

  var obj2 = {a : 0, b : 1, c : 2, d : 3, e : 4, f : 5};
  if (Object.every.call(obj2, nr_lt_5)) {
    $t.report_error('Object.every.call(obj2, nr_lt_5) failed.');
  }


  $t.output('Testing .forEach() ...');

  obj1 = {1  : 'a', b : 2, 3 : 'c', d : 4};
  obj2 = {};

  Object.forEach.call(obj1, function (val, i, obj) {
    obj2[i] = obj[i];
  });

  // Check result using .every() that the values are the same in obj2.
  var res = Object.every.call(obj2, function (val, i) {
    return obj1[i] === val;
  });

  if (!res) {
    $t.report_error('Object.forEach.call(obj2, ...) failed.');
  }


  $t.output('Testing .some() ...');

  obj1 = {a : 10, b : 23, c : 34, d : 4, e : 5};
  if (!Object.some.call(obj1, nr_lt_5)) {
    $t.report_error('Object.some.call(obj1, nr_lt_5) failed.');
  }

  obj2 = {a : 10, b : 23, c : 34, d : 41, e : 5};
  if (Object.some.call(obj2, nr_lt_5)) {
    $t.report_error('Object.some.call(obj2, nr_lt_5) failed.');
  }

  $t.output('Testing .filter() ...');

  obj1 = {a : 5, b : 3, c : 7, d : 8, e : 4, f : 10, g : 1};
  obj2 = Object.filter.call(obj1, nr_lt_5);
  if (obj2.b != 3 || obj2.e != 4 || obj2.g != 1
      || !Object.every.call(obj2, nr_lt_5)) {
    $t.report_error('Object.filter.call(obj1, nr_lt_5) failed.');
  }

  $t.output('Testing .map() ...');

  var map_fn = function (val, i) {
    return val*i.charCodeAt(0);
  };

  obj2 = Object.map.call(obj1, map_fn);
  res = Object.every.call(obj1, function (val, i) {
    return obj2[i] === val*i.charCodeAt(0);
  });

  if (!res) {
    $t.report_error('Object.map.call(obj1, map_fn) failed.');
  }

  $t.output('Testing .reduce() ...');

  var sum_fn = function (last, val, i) {
    return last+val+i;
  };

  obj1 = {a : 0, b : 1, c : 2, d : 3};
  if (Object.reduce.call(obj1, sum_fn, '') != '0a1b2c3d') {
    $t.report_error('Object.reduce.call(obj1, sum_fn) failed.');
  }
};

/*
 * Test the String methods, native or not.
 */
tests.corejs_string = function () {
  $t.output('Testing .trim(), .trimLeft() and .trimRight() ...');

  var str = '  test  ';
  if (str.trim() != 'test') {
    $t.report_error('str.trim() failed.');
  }
  if (str.trimLeft() != 'test  ') {
    $t.report_error('str.trimLeft() failed.');
  }
  if (str.trimRight() != '  test') {
    $t.report_error('str.trimRight() failed.');
  }

  $t.output('Testing .escapeXML() and .unescapeXML() ...');

  str = '<a href="http://example.com">Test &amp; more.</a> ' +
    '&lt;test attr=&quot;val"&gt;';

  var str2 = '&lt;a href=&#34;http://example.com&#34;&gt;Test &amp;amp; ' +
    'more.&lt;/a&gt; &amp;lt;test attr=&amp;quot;val&#34;&amp;gt;';

  if (str.escapeXML() != str2) {
    $t.report_error("str.escapeXML() failed.");
  }
  if (str2.unescapeXML() != str) {
    return "str2.unescapeXML() failed.";
  }

  $t.output('Testing .stripTags() ...');

  str = '<a href="http://example.com">Test</a> ' +
    '<strong>strip tags</strong>';

  str2 = 'Test strip tags';
  var str3 = 'Test <strong>strip tags</strong>';

  if (str.stripTags() != str2) {
    $t.report_error('str.stripTags() failed.');
  }
  if (str.stripTags('a') != str3) {
    $t.report_error("str.stripTags('a') failed.");
  }
  if (str.stripTags('a', 'strong') != str2) {
    $t.report_error("str.stripTags('a', 'strong') failed.");
  }
};
    // --></script>
  </head>
  <body>
  </body>
  <!-- vim:set spell spl=en fo=tcroqwanl1 tw=80 ts=2 sw=2 sts=2 sta et ai cin ff=unix: -->
</html>
